package view;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.util.concurrent.Semaphore;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import entidades.Processo;
import entidades.Recurso;
import entidades.Sistema;
import entidades.Vertice;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Main extends javax.swing.JFrame implements Runnable{

	public static SparseMultigraph<Vertice,String> graph;
	private JLabel jLabel1;
	private static  Main instance;
	private PanelGrafo grafoPanel;
	public static Semaphore main;
	public static Semaphore mutexLog = new Semaphore(1);

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main inst = new Main();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public Main() {
		super();
		graph= new SparseMultigraph<Vertice,String>();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		initGUI();
		Thread t = new Thread(this);
		t.start();
	}

	private void initGUI() {

		this.setLayout(null);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setFocusTraversalKeysEnabled(false);
		pack();
		this.setSize(1054, 726);

		PanelRecurso panelRecurso = new PanelRecurso();
		getContentPane().add(panelRecurso, BorderLayout.NORTH);
		panelRecurso.setBounds(3, 404, 195, 136);
		panelRecurso.setBorder(BorderFactory.createTitledBorder("Recurso"));

		PanelProcesso panelProcesso = new PanelProcesso();
		getContentPane().add(panelProcesso, BorderLayout.NORTH);
		panelProcesso.setBounds(3, 551, 195, 134);
		panelProcesso.setBorder(BorderFactory.createTitledBorder("Processo"));

		PanelLog pLog = new PanelLog();
		getContentPane().add(pLog, BorderLayout.NORTH);
		pLog.setBorder(BorderFactory.createTitledBorder("Log"));
		pLog.setBounds(204, 513, 824, 179);
		
		PanelSistema pSis = new PanelSistema();
		getContentPane().add(pSis, BorderLayout.NORTH);
		pSis.setBorder(BorderFactory.createTitledBorder("Sistema"));
		pSis.setBounds(4, 300, 196, 89);

		grafoPanel = new PanelGrafo();
		grafoPanel.setBounds(203, 2, 825, 511);
		grafoPanel.setOpaque(false);
		grafoPanel.setBorder(BorderFactory.createTitledBorder("Grafo"));
		getContentPane().add(grafoPanel, BorderLayout.NORTH);
		{
			jLabel1 = new JLabel();
			getContentPane().add(jLabel1);
			jLabel1.setBounds(3, 9, 200, 285);
			jLabel1.setIcon(new ImageIcon(getClass().getClassLoader().getResource("107.png")));
		}

		instance = this;
	}

	public static Main getInstance() {
		return instance;
	}
	
	public static void PosicaoProcesso() {
		int defaultX=60;
		int defaultY=50;
		for(int i=0;i<Sistema.processos.size();i++){
			if(i==0){
				Sistema.processos.get(i).setPosX(defaultX);Sistema.processos.get(i).setPosY(defaultY);
			}else
				Sistema.processos.get(i).setPosX(defaultX+=80);Sistema.processos.get(i).setPosY(defaultY);
		}

	}
	
	public static void PosicaoRecurso() {
		int defaultX=60;
		int defaultY=400;
		for(int i=0;i<Sistema.recursos.size();i++){
			if(i==0){
				Sistema.recursos.get(i).setPosX(defaultX);Sistema.recursos.get(i).setPosY(defaultY);
			}else
				Sistema.recursos.get(i).setPosX(defaultX+=80);Sistema.recursos.get(i).setPosY(defaultY);
		}

	}
	
	/**
	 * M�todo para adicionar um recurso no grafo
	 * @param recursos
	 * @param processos
	 */
	public void addRecurso(final Recurso recurso){
			graph.addVertex(recurso);
			grafoPanel.initGUI();
	}
	
	/**
	 * M�todo para adicionar um processo no grafo
	 * @param recursos
	 * @param processos
	 */
	public void addProcesso(final Processo processo){
			graph.addVertex(processo);
			grafoPanel.initGUI();
	}
	
	public void SolicitaRecurso(final Processo processo,final Recurso recurso){
		graph.addEdge(processo.getIdentificador()+""+recurso.getIdentificador(),processo,recurso, EdgeType.DIRECTED);
	}
	
	public void removeRecursoSolicitado(final Processo processo, final Recurso recurso){
		graph.removeEdge(processo.getIdentificador()+""+recurso.getIdentificador());
	}
	
	public void obterRecurso(final Recurso recurso,final Processo processo){
		graph.addEdge(recurso.getIdentificador()+""+processo.getIdentificador(),recurso,processo, EdgeType.DIRECTED);
	}
	public void removeRecursoObtido(final Recurso recurso,final Processo processo){
		graph.removeEdge(recurso.getIdentificador()+""+processo.getIdentificador());
	}

	@Override
	public void run() {
		while(true){
			try {
				Thread.sleep(500);
				grafoPanel.initGUI();
				grafoPanel.updateUI();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
